<template>
  <div class="app-container">
    <el-card>
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item label="关键字:">
          <el-input v-model="queryForm.keyword" placeholder="请输入关键字" style="width: 128px;" prefix-icon="el-icon-search"
                    size="small"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="fetchData(1)">查询</el-button>
        </el-form-item>
        <el-form-item style="flex-grow: 1;" />
        <el-form-item style="margin-right: 0;">
          <el-button type="primary" icon="el-icon-plus" size="small" @click="showNewDepartmentDialog()">新建</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        stripe
        highlight-current-row
      >
        <el-table-column align="center" label="编号" width="164">
          <template v-slot="scope">
            {{ scope.row.gid }}
          </template>
        </el-table-column>
        <el-table-column label="部门">
          <template v-slot="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column class-name="status-col" label="操作" align="center" width="96">
          <template v-slot="scope">
            <el-button size="mini" icon="el-icon-delete" type="danger" :underline="false" @click="showDelDialog(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="width: 100%;text-align: center;margin:24px 0 0 0;">
        <el-pagination
          :page-size="pageSize"
          background
          layout="prev, pager, next"
          :current-page.sync="currentPage"
          :total="total"
          @current-change="fetchData(currentPage)"
        />
      </div>
    </el-card>
    <el-dialog
      title="警告！"
      :visible.sync="delDialogVisible"
      width="30%"
      center
    >
      <span style="width:100%;text-align: center;">{{ `部门${toDelDepartmentInfo.gid}将被删除` }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialogVisible = false">取 消</el-button>
        <el-button type="danger" @click="confirmDelete()">删 除</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="新建部门"
      :visible.sync="addNewDepartmentDialog"
      width="30%"
    >
      <el-form :model="form">
        <el-form-item label-position="right" label="部门名称:">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button style="margin-left: auto;" @click="addNewDepartmentDialog = false">取 消</el-button>
        <el-button type="primary" @click="addNewDepartment()">新 建</el-button>
      </span>

    </el-dialog>
  </div>
</template>

<script>
import { addDepartment, getDepartmentList } from '@/api/department.js'
import { PAGE_SIZES } from '@/utils/common'
import { delDepartment } from '../../api/department'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      form: {
        name: ''
      },
      addNewDepartmentDialog: false,
      toDelDepartmentInfo: -1,
      delDialogVisible: false,
      currentPage: 1,
      pageSize: PAGE_SIZES,
      total: 0,
      queryForm: {
        keyword: '',
        status: '',
        dateRange: []
      },
      list: null,
      listLoading: true,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    this.fetchData(1)
  },
  methods: {
    fetchData(page) {
      this.listLoading = true
      const params = {
        name: this.queryForm.keyword
      }
      getDepartmentList(this.pageSize, page, params).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    viewOrder() {
    },
    addNewDepartment() {
      addDepartment({ name: this.form.name }).then(res => {
        this.$message.success('新建成功！')
        this.addNewDepartmentDialog = false
        this.fetchData(1)
      })
    },
    showNewDepartmentDialog() {
      this.addNewDepartmentDialog = true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    showDelDialog(info) {
      this.toDelDepartmentInfo = info
      this.delDialogVisible = true
    },
    confirmDelete() {
      // this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   this.$message({
      //     type: 'success',
      //     message: '删除成功!'
      //   })
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消删除'
      //   })
      // })
      delDepartment(this.toDelDepartmentInfo.gid).then(res => {
        this.$message.success('删除成功!')
        setTimeout(() => {
          this.fetchData(1)
        }, 500)
        this.delDialogVisible = false
      }).catch(() => {
        this.$message.error('删除失败')
        this.delDialogVisible = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .demo-form-inline{
    display: flex;
  }
</style>
